package me.zhengjie.modules.service.impl;

import cn.hutool.core.date.DateTime;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.domain.Api;
import me.zhengjie.modules.domain.ApiBatch;
import me.zhengjie.modules.domain.Batch;
import me.zhengjie.modules.domain.Companies;
import me.zhengjie.modules.domain.enterprise.EnterpriseFinancing;
import me.zhengjie.modules.domain.enterprise.EnterpriseInformation;
import me.zhengjie.modules.domain.enterprise.EnterpriseProduct;
import me.zhengjie.modules.repository.ApiBatchMapper;
import me.zhengjie.modules.repository.ApiMapper;
import me.zhengjie.modules.repository.BatchMapper;
import me.zhengjie.modules.repository.CompaniesMapper;
import me.zhengjie.modules.repository.enterprise.EnterpriseFinMapper;
import me.zhengjie.modules.repository.enterprise.EnterpriseInfMapper;
import me.zhengjie.modules.service.EnterpriseFinService;
import me.zhengjie.modules.utils.Contents;
import me.zhengjie.modules.utils.JsonUtils;
import me.zhengjie.modules.utils.MyHttpUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("EnterpriseFinService")
@RequiredArgsConstructor
public class EnterpriseFinServiceImpl implements EnterpriseFinService {

    private final EnterpriseFinMapper enterpriseFinMapper;
    private final CompaniesMapper companiesMapper;

    private final ApiMapper apiMapper;
    private final BatchMapper batchMapper;
    private final ApiBatchMapper apiBatchMapper;


    /**
     * 批量添加企业融资历史信息
     * @param list
     * @return
     */
    @Transactional
    @Override
    public int addEnterpriseFinByList(List<EnterpriseFinancing> list) {
        return enterpriseFinMapper.insertEnterpriseFinByList(list);
    }

    //@Scheduled(fixedDelay = 1000*24*60*60,initialDelay=1000*60*4)
    @Transactional
    public void AutoAddEnterpriseFinByList(){
        Api api=apiMapper.selectApiByVariableName("BASIC_ENTERPRISE_FIN");
        ApiBatch apiBatches=apiBatchMapper.selectApiBatchByApiId(api.getId());
        if(apiBatches!=null){
            //apiBatches不为空的情况，说明有查到数据，在这个基础上判断是否满足执行周期
            //满足执行周期则重新获取一次
            //不满足则跳过 结束方法
            Date now=new Date();
            long poor=now.getTime()-apiBatches.getCreateTime().getTime();
            System.out.println(poor);
            long day= (long)api.getCycle()*24*60*60*1000;
            System.out.println(day);
            if(poor>day){
                //到执行周期了
                System.out.println("执行新一轮的企业融资历史数据添加");
                Long batchId= util.SnowFlakeUtil.getSnowFlakeId();
                //从数据库中查询最新的一批企业基本信息 进行关联
                List<Companies> companiesList=companiesMapper.selectAll();
                if(companiesList!=null&&companiesList.size()>0){
                    for (Companies c : companiesList) {
                        //List<EnterpriseFinancing> financingList=JsonUtils.JsonToEnterpriseFinList(MyHttpUtils.GetJson(Contents.token,"http://open.api.tianyancha.com/services/open/cd/findHistoryRongzi/2.0?pageSize=20&keyword="+c.getCompanyName()+"&pageNum=1"),c.getCompanyName());
                        List<EnterpriseFinancing> financingList=JsonUtils.JsonToEnterpriseFinList(Contents.a);
                        if(financingList!=null&&financingList.size()>0){
                            financingList.forEach(i->{
                                i.setCompaniesId(c.getId());
                                i.setBatchId(batchId);
                            });
                            enterpriseFinMapper.AutoInsertEnterpriseFinByList(financingList);
                        }
                    }
                    Batch batch=new Batch();
                    batch.setBatchNo(batchId);
                    batch.setCreateTime(new DateTime());
                    batchMapper.insertBatch(batch);
                    ApiBatch apiBatch=new ApiBatch();
                    apiBatch.setApiId(api.getId());
                    apiBatch.setBatchId(batchId);
                    apiBatch.setCreateTime(new DateTime());
                    apiBatchMapper.insertApiBatch(apiBatch);
                }
            }else{
                //执行周期没到，直接结束方法运行
                return;
            }
        }else{
            //apiBatches为空的情况 说明这个数据集是第一次执行，数据库中没有数据
            //直接从数据库中获取最新的企业基本信息  作为条件查询
            //这种情况要从天眼查获取第一轮数据
            System.out.println("暂无数据");
            Long batchId= util.SnowFlakeUtil.getSnowFlakeId();
            //从数据库中查询最新的一批企业基本信息 进行关联
            List<Companies> companiesList=companiesMapper.selectAll();
            if(companiesList!=null&&companiesList.size()>0){
                for (Companies c : companiesList) {
                    //List<EnterpriseFinancing> financingList=JsonUtils.JsonToEnterpriseFinList(MyHttpUtils.GetJson(Contents.token,"http://open.api.tianyancha.com/services/open/cd/findHistoryRongzi/2.0?pageSize=20&keyword="+c.getCompanyName()+"&pageNum=1"),c.getCompanyName());
                    List<EnterpriseFinancing> financingList=JsonUtils.JsonToEnterpriseFinList(Contents.a);
                    if(financingList!=null&&financingList.size()>0){
                        financingList.forEach(i->{
                            i.setCompaniesId(c.getId());
                            i.setBatchId(batchId);
                        });
                        enterpriseFinMapper.AutoInsertEnterpriseFinByList(financingList);
                    }
                }
                Batch batch=new Batch();
                batch.setBatchNo(batchId);
                batch.setCreateTime(new DateTime());
                batchMapper.insertBatch(batch);
                ApiBatch apiBatch=new ApiBatch();
                apiBatch.setApiId(api.getId());
                apiBatch.setBatchId(batchId);
                apiBatch.setCreateTime(new DateTime());
                apiBatchMapper.insertApiBatch(apiBatch);
            }
        }
    }

    @Override
    public List<EnterpriseFinancing> queryEnterpriseFinByName(String name) {
        return enterpriseFinMapper.selectEnterpriseFinByName(name);
    }

    @Override
    public List<EnterpriseFinancing> queryEnterpriseFinForPageByConditions(Map<String, Object> map) {
        return enterpriseFinMapper.selectEnterpriseFinForPageByConditions(map);
    }

    @Override
    public int queryEnterpriseFinForPageByConditionsToTotal(Map<String, Object> map) {
        return enterpriseFinMapper.selectEnterpriseFinForPageByConditionsToTotal(map);
    }

    @Override
    public List<String> queryRound() {
        return enterpriseFinMapper.selectRound();
    }

    @Override
    public List<String> queryPubTime() {
        return enterpriseFinMapper.selectPubTime();
    }
}
